package com.sunducation.wts.paging

import android.view.ViewGroup
import androidx.paging.PagingDataAdapter
import androidx.recyclerview.widget.DiffUtil
import com.sunducation.wts.paging.MyDataBean
import com.sunducation.wts.paging.MyViewHolder
import android.view.LayoutInflater



class MyPaged3ListAdapter : PagingDataAdapter<MyDataBean, MyViewHolder>(DIFF_CALLBACK) {
    companion object{
        val DIFF_CALLBACK: DiffUtil.ItemCallback<MyDataBean> = object : DiffUtil.ItemCallback<MyDataBean>(){
            override fun areItemsTheSame(oldItem: MyDataBean, newItem: MyDataBean): Boolean {
                // 比较两个Item数据是否一样的，可以简单用 Bean 的 hashCode来对比
                return oldItem.hashCode() == newItem.hashCode();
            }

            override fun areContentsTheSame(oldItem: MyDataBean, newItem: MyDataBean): Boolean {
                // 写法基本上都这样写死即可
                return oldItem.equals(newItem);
            }

        }
    }
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
        val inflate = LayoutInflater.from(parent.context).inflate(R.layout.item_my, parent, false)
        return MyViewHolder(inflate)
    }

    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        // getItem() 是 PagedListAdapter 内部方法，通过此方法可以获取到对应位置Item 的数据
        holder.bindData(getItem(position)!!);
    }


}